﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using ISS.CaseStudy.BLL;

namespace CaseStudy.AdminPortal
{
    public partial class UserManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            loginInfo.Login();

            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void gvUserInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect("UserInfoDetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=edit"); 
            }
            else if (e.CommandName.ToLower() == "delete")
            {
                UserInformationBLL.Delete(e.CommandArgument.ToString());
                BindData();
                Response.Redirect(this.Request.Url.ToString());
            }
            else if (e.CommandName.ToLower() == "view")
            {
                Response.Redirect("UserInfoDetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=view"); 
            }
        }

        protected void gvUserInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvUserInfo.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void imgBtnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserInfoDetail.aspx");
        }

        private void BindData()
        {
            DataTable dataTable = UserInformationBLL.GetAll().Tables[0];
            foreach (DataRow row in dataTable.Rows)
            {
                row["Name"] = HttpUtility.HtmlDecode(row["Name"].ToString());
                row["Password"] = HttpUtility.HtmlDecode(row["Password"].ToString());
                row["NickName"] = HttpUtility.HtmlDecode(row["NickName"].ToString());
            }

            gvUserInfo.DataSource = dataTable.DataSet;
            gvUserInfo.DataBind();
        }

    }
}
